package com.iwomy.secureplat.platform.transmission;

import com.iwomy.secureplat.platform.transmission.parse.EmpaerDataParse;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author ruanwb
 * @Describe 环保设备对接程序启动类，Netty服务器端启动类，接收设备上传的数据。
 * @Date 2023-9-28 17:44:05
 */
@Slf4j
@Component
@Order(1)
public class EPServer implements ApplicationRunner {

	/**
	 * Netty服务端监听的端口号
	 */
	public static final int PORT = 8083;

	/**
	 * 分发线程组
	 */
	private static final EventLoopGroup bossGroup = new NioEventLoopGroup(2);

	/**
	 * 工作线程组
	 */
	private static final EventLoopGroup workerGroup = new NioEventLoopGroup(4);


	/**
	 * 启动服务
	 */
	public static void runEPServer() {
		EmpaerDataParse empaerDataParse = new EmpaerDataParse();
		DrcProInfo drcProInfo = new DrcProInfo();
		drcProInfo.setPort(8083);
		empaerDataParse.setDrcProInfo(drcProInfo);
		DataRecvServer server = new DataRecvServer(PORT, "Empaer",empaerDataParse);
		try {
			server.start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 关闭服务
	 */
	public static void shutdown() {
		// 优雅关闭
		workerGroup.shutdownGracefully();
		bossGroup.shutdownGracefully();
	}

	@Override
	public void run(ApplicationArguments args) {
		// 启动环保监测Netty服务端
		runEPServer();
	}

}
